package cn.edu.njupt.post.service;


import cn.edu.njupt.post.dao.PostDAO;
import cn.edu.njupt.post.model.Post;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import java.util.List;

@Service
public class PostService {
    @Autowired
    PostDAO postDAO;

    public Post getById(int id) {
        return postDAO.getById(id);
    }

    public int addPost(Post post) {
        post.setTitle(HtmlUtils.htmlEscape(post.getTitle()));
        post.setContent(HtmlUtils.htmlEscape(post.getContent()));

        post.setTitle(post.getTitle());
        post.setContent(post.getContent());
        return postDAO.addPost(post) > 0 ? post.getId() : 0;
    }

    public int updateCommentCount(int id, int count) {
        return postDAO.updateCommentCount(id, count);
    }

    public List<Post> getLatestPosts(int offset, int limit) {
        return postDAO.selectLatestPosts(offset, limit);
    }

    public List<Post> getLatestPostsByUser(int userId, int offset, int limit) {
        return postDAO.selectLatestPostsByUser(userId, offset, limit);
    }

    public List<Post> getLatestPostsByUsers(List<Integer> userIds, int count) {
        return postDAO.selectLatestPostsByUsers(userIds, count);
    }

    public List<Post> getHotPosts(int offset, int limit) {
        return postDAO.selectHotPosts(offset, limit);
    }

    public int getPostCount() {return postDAO.getPostCount();}

    public int getPostCountById(int userId) {return postDAO.getPostCountById(userId);}

    public int deleteById(int id) {return postDAO.deleteById(id);}
}
